package readers;

import containers.Groups;
import containers.TestScores;
import containers.TestsTaken;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
import validator.validTeacher;

/**
 * Handles all the reads from the harddrive
 * This is a static class and will be used mainly for 
 * reading and putting the information into usable
 * linklists
 * @author Chris
 */
public class UserReader extends Reader {
    protected UserReader() { }
       
    //returns a password
    public static String getPassword(String userName) {
        //Open file userName_password.txt
        if(!Reader.fileExists(readLocation + userName+"_password.txt"))
            return "";
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + userName+"_password.txt"));
            
            line = br.readLine();
            br.close();
            
            return line;
        }
        catch(Exception ex) {
            return "";
        }
    }
    
    //Check if the user is a teacher
    public static boolean isTeacher(String teacherName) {
        validTeacher vt = new validTeacher();
        return vt.isValid(teacherName);
    }
    
    //Pass in a link list that will be filled.
    //The return result will be whether the link list was filled
    public static boolean getStudentsGroups(String userName, LinkedList<Groups> lst) {
        if(!Reader.fileExists(readLocation + userName + "_belongs.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + userName+"_belongs.txt"));
            
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() >= 2)
                    lst.addLast(new Groups(st.nextToken(), st.nextToken())); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    //Pass in a link list that will be filled.
    //The return result will be whether the link list was filled
    public static boolean getTakenTests(String userName, LinkedList<TestsTaken> lst) {
        if(!Reader.fileExists(readLocation + userName + "_taken.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + userName+"_taken.txt"));
            
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() >= 4)
                    lst.addLast(new TestsTaken(st.nextToken(), st.nextToken(), st.nextToken(), st.nextToken())); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    //The processing will be done in a higher function to get the teacherName
    public static boolean getTests(String teacherName, String groups, LinkedList<String> lst) {
        if(!Reader.fileExists(readLocation + teacherName + "_" + groups + "_tests.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + teacherName + "_" + groups + "_tests.txt"));
            
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() == 1)
                    lst.addLast(st.nextToken()); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    //The processing will be done in a higher function to get the teacherName
    public static boolean getTest(String teacherName, String groups,
                                  String test, LinkedList<String> lst) {
        if(!Reader.fileExists(readLocation + teacherName + "_" + groups + "_" + test + "_words.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + teacherName + "_" + groups + "_" + test + "_words.txt"));
            
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() == 1)
                    lst.addLast(st.nextToken()); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }

    //The processing will be done in a higher function to get the teacherName
    public static boolean getTestScores(String teacherName, String groups,
                                  String test, LinkedList<TestScores> lst) {
        if(!Reader.fileExists(readLocation + teacherName + "_" + groups + "_" + test + "_score.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + teacherName + "_" + groups + "_" + test + "_score.txt"));
            
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() >= 2)
                    lst.addLast(new TestScores(st.nextToken(), st.nextToken())); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    //Returns all the groups owned by a teacher
    public static boolean getTeacherGroups(String teacherName, LinkedList<String> lst) {
        if(!Reader.fileExists(readLocation + teacherName + "_teacher_groups.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + teacherName + "_teacher_groups.txt"));
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() == 1)
                    lst.addLast(st.nextToken()); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
    
    //List of all student names who get access to a teachers group
    public static boolean getTeacherGroupAccess(String teacherName, String group
            , LinkedList<String> lst) {
        if(!Reader.fileExists(readLocation + teacherName + "_" + group + "_access_groups.txt")) {
            return false;
        }
        
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(readLocation + teacherName + "_" + group + "_access_groups.txt"));
            
            while((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                if(st.countTokens() == 1)
                    lst.addLast(st.nextToken()); 
            }
            br.close();
            
            return true;
        }
        catch(Exception ex) {
            return false;
        }
    }
}
